package aom.ju.ss.view.user.userOrder

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.ListBinding
import aom.ju.ss.databinding.UserOrderListItemBinding
import aom.ju.ss.mode.GoodsOrderMode
import aom.ju.ss.mode.GoodsOrderModeData
import aom.ju.ss.mode.TuikuangoodsInfo
import aom.ju.ss.utils.Fin
import aom.ju.ss.utils.OrderBtList
import aom.ju.ss.utils.VG
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseFragment
import aom.ju.ss.view.base.PAdapter
import com.google.gson.Gson
import java.lang.Exception

class UserOrderFragment : BaseFragment() {
    //订单类型  1普通订单  2砍价订单  3抢券订单  4易货订单  5易货促销订单
    //状态  1待支付  2待发货  3待签收  4待评论
    companion object {
        fun getMys(type: String): UserOrderFragment {
            val f = UserOrderFragment()
            val b = Bundle()
            b.putString("type", type)
            f.arguments = b
            return f
        }
    }

    override fun onResume() {
        super.onResume()
        try {
            binding.small.autoRefresh()
        } catch (ex: Exception) {
            ex.message?.show
        }
    }

    private lateinit var binding: ListBinding
    private lateinit var pAdapter: PAdapter
    private var list: MutableList<GoodsOrderModeData> = arrayListOf()
    override fun init() {
        binding = viewBinding as ListBinding
        type = requireArguments().getString("type", "1")
        pAdapter = PAdapter(
            list, R.layout.user_order_list_item
        ) { bb, position ->
            val b = bb as UserOrderListItemBinding
            b.info = list[position]
            val info = list[position]
            b.rec.info = info.goods_list

            OrderBtList(
                TuikuangoodsInfo(
                    info.goods_list.goods_image,
                    info.goods_list.goods_name,
                    info.goods_list.sku_name,
                    info.goods_list.num.toString(),
                    info.goods_list.price
                ),
                info.type.toString(),
                info.unpaid,
                info.id,
                info.after_sale_status,
                info.status,
                b.olb,
                activity,
                binding.small
            )

//            b.rec.layoutManager = LinearLayoutManager(activity)
//            b.rec.adapter = PAdapter(
//                list[position].goods_list,
//                R.layout.user_order_goods_list_item,
//                object : BindViewInterface {
//                    override fun bandView(binding: ViewDataBinding?, position: Int) {
//                    }
//                })

        }

        binding.mRecycler.layoutManager = LinearLayoutManager(activity)
        binding.mRecycler.adapter = pAdapter

        binding.small.setOnRefreshListener {
            page = 1
            data
        }
        binding.small.setOnLoadMoreListener {
            page++
            data
        }
        data
    }


    var page = 1
    var type = "0"
    val data: Unit
        get() {
            OKHttpUtils(activity, false)
                .SetApiUrl(LP_API.order_list)
                .SetKey("type", "status", "page", "after_sale_status")
                .SetValue("", type, page.toString(), if (type == "5") "1" else "")
                .POST(this)
        }

    override fun ok(jsonString: String?, httpTY: String?) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, GoodsOrderMode::class.java)
        if (page == 1) {
            list.clear()
        }
        mode.data?.let {
            list.addAll(it)
        }
        binding.noData.VG(list.size == 0)
        binding.msg.text = "暂无订单数据~"
        pAdapter.notifyDataSetChanged()
        binding.small.Fin()

    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        e?.show
        binding.small.Fin()
    }

    override fun getLay(): Int {
        return R.layout.list
    }
}